package cn.net.isir.controller.manager;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.core.BasicController;
import cn.net.isir.core.BasicModel;
import cn.net.isir.core.SetBasicService;
import cn.net.isir.entity.Category;
import cn.net.isir.entity.Shop;
import cn.net.isir.service2.CategoryService;
import cn.net.isir.service2.ShopService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxubo
 * @filename shopController.java
 * @package cn.net.isir.controller.manager
 * @email lxb@isir.net.cn
 * @date 2018/11/28
 * @comment
 * @since 1.8
 */

@RestController
@RequestMapping("/shop")
public class ShopController extends BasicController<Shop> {

    @SetBasicService
    @Resource
    private ShopService shopService;
    @Resource
    private CategoryService categoryService;

    @RequestMapping(value = "/shopCateFilter", method = RequestMethod.GET)
    public ResultObject shopCateFilter() {
        BasicModel basicModel = new BasicModel();
        basicModel.setQueryUserId(TokenManager.getTokenId());
        PageInfo pageInfo = shopService.selectListByUser(basicModel);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<Shop> shopList = pageInfo.getList();
        //循环插入json
        shopList.forEach(item -> {
            JSONObject jsonObject = JSONObject.parseObject(item.toString());
            jsonObject.put("label", item.getName());
            jsonObject.put("value", item.getId());
            Category category = new Category();
            category.setShopId(item.getId());
            List<JSONObject> categoryList = new ArrayList<>();
            categoryService.selectList(category).forEach(item2 -> {
                JSONObject jsonObject2 = JSONObject.parseObject(item2.toString());
                jsonObject2.put("label", item2.getName());
                jsonObject2.put("value", item2.getId());
                categoryList.add(jsonObject2);
            });
            jsonObject.put("children", categoryList);
            jsonObjectList.add(jsonObject);
        });
        return new ResultObject(200, "success", jsonObjectList);
    }

}
